package com.sunyzc.ebaotech.simulator.soap;

import java.util.ArrayList;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class SOAPClient {
	private static final Logger log = LoggerFactory.getLogger(SOAPClient.class);
	private static final String BASE_PACKAGE = "com.sunyzc.ebaotech.simulator.soap.";

	public static void main(String[] args) {
		String serverHost = ISOAPProcessor.SERVER_HOST_SHVM00310;
		String currentDate = null;
		int totalCount = 1;
		requesAllLSWebService(serverHost, currentDate, totalCount);
	}

	private static void requesAllLSWebService(String serverHost, String currentDate, int totalCount) {
		requestLSWebService(serverHost, "WANGXIAO", currentDate, totalCount);
	}

	private static void requestLSWebService(String serverHost, String clientCode, String currentDate, int totalCount) {
		if (clientCode != null && !clientCode.trim().equals("")) {
			List<ISOAPProcessor> processorList = new ArrayList<ISOAPProcessor>();
			try {
				for (int index = 1; index <= totalCount; index++) {
					ISOAPProcessor processor = generateProcessor(clientCode);
					log.info(processor.getClass().getSimpleName() + " 第 " + index + "/" + totalCount + " 次开始");
					processor.setCurrentDate(currentDate);
					processorList.add(processor);
					if (clientCode.equals("WANGXIAO")) {
						requestLSWebService(serverHost, processor, "New Underwriting", null);
						requestLSWebService(serverHost, processor, "Policy Value Inquiry", null);
						requestLSWebService(serverHost, processor, "Surrender Inquiry", null);
						requestLSWebService(serverHost, processor, "Surrender", null);
						// 其他交易类型
					} else {
						// 其他交易渠道
					}
					log.info(processor.getClass().getSimpleName() + " 第 " + index + "/" + totalCount + " 次结束");
				}
			} catch (Throwable e) {
				log.error(e.getMessage(), e);
			}
		}
	}

	public static String requestLSWebService(String serverHost, ISOAPProcessor processor, String requestTransType, String requestBodyTemplateStr) throws Exception {
		long startTime = System.currentTimeMillis();
		String requestBodyContentStr = processor.generateRequestBodyStr(requestTransType, requestBodyTemplateStr, true);
		log.info("交易[" + requestTransType + "]请求报文体: " + requestBodyContentStr);
		String resultStr = processor.invokeSOAPServer(serverHost, requestTransType, requestBodyContentStr);
		log.info("交易[" + requestTransType + "]响应报文体: " + resultStr.toString());
		// log.info(HttpUtil.httpRequest("http://172.25.12.29:7001/ls/services/NetSalesExchange", "GET", requestBodyContentStr, "UTF-8"));
		log.info("交易[" + requestTransType + "]用时 " + (System.currentTimeMillis() - startTime) + " 毫秒");
		return resultStr;
	}

	public static ISOAPProcessor generateProcessor(String clientCode) throws Exception {
		return (ISOAPProcessor) Class.forName(BASE_PACKAGE + clientCode.toLowerCase() + "." + clientCode + "Processor").newInstance();
	}
}
